<?php

namespace App\Http\Controllers\Mobile\Personal;
use App\Http\Requests\Personal\ModPassword\ModPasswordRequest;
use App\Models\Personal\PersonalUser;
use Illuminate\Http\Request;
use Style,CustomPassword;

class PersonalUserController extends CommonController{
    /**
     * 账号设置
     * @return mixed
     */
    public function setAccount(){
        return Style::view('mobile.personal.setAccount');
    }

    /**
     * 修改密码
     * @return mixed
     */
    public function modPassword(){
        $user = auth()->guard('personal')->user();
        $uid = $user->uid;
        return Style::view('mobile.personal.modPassword', compact('uid'));
    }

    /**
     * 修改密码提交
     * @param ModPasswordRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function postModPassword(ModPasswordRequest $request){
        $input['password'] = CustomPassword::make($request->input('password'));
        $result = PersonalUser::where('uid', $request->uid)->update($input);
        if($result){
            return response()->json(['status'=>0,'msg'=>'修改密码成功']);
        }else{
            return response()->json(['status'=>1,'msg'=>'修改密码失败']);
        }
    }

    /**
     * 修改密码提交
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function ajaxPass(Request $request){
        $user = auth()->guard('personal')->user();
        $password = PersonalUser::where('uid', $user->uid)->first(['password'])->password;
        //判断旧密码是不是正确
        if(CustomPassword::check($request->input('oldpassword'),$password)){
            return response()->json(['status'=>0,'msg'=>'旧密码正确']);
        }else{
            return response()->json(['status'=>1,'msg'=>'旧密码不正确']);
        }
    }

}